<?php
/*
 * Created on Mar 27, 2009
 * author: ANH
 * fileName: IndexController.php
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

 class Gallery_DevelController extends Clu_Controller_Action_Master
{
	protected $m;
	public function init()
	{
		//require_once  (APP_PATH.'/modules/place/Model/Place.php');
		//require_once  (APP_PATH.'/modules/user/Model/User.php');
		//require_once  (APP_PATH.'/modules/article/Model/Article.php');
		//require_once  (APP_PATH.'/modules/node/Model/NodeService.php');
		$this->mPlace = new place_Model_Place();
		//require_once  (APP_PATH.'/modules/event/Model/Event.php');
		$this->mEvent = new event_Model_Service();
		$this->mArticle = new article_Model_Service();
		$this->m = new gallery_Model_Gallery();
		parent::init();
	}

	public function updateEventGalleryAction()
	{
		$db = Zend_Db_Table::getDefaultAdapter() ;
	
    	$id = 0;
	    $uModel = new user_Model_User();

    	for ($id = 0; $id < 12000; $id++)
    	{
    		$p = $this->mEvent->findByPrimary($id);
    		if (!$p || $p['checked'])
    			continue;
	    	$posterId = $p['poster_id'];
	    	$u = $uModel->findByPrimary($posterId);
	    	
	    	$article = $this->mArticle->findByPrimary($p['article_id']);
	    	///public/ufiles/17/images/medium/event/14_choi_trau.jpg
	    	$relativeDir = "/public/ustore/$posterId/images/original/event/";
    		$dir = APP_PATH. $relativeDir;
			if(file_exists($dir))
			{
			    $files = scandir($dir);
			    $displayFiles = array();
			    foreach ($files as $f)
			    {
			    	if (strpos ($f, $id ."_") === 0)
			    	{
	 		    		$ctime = filectime($dir . $f); 
	 		    		$displayFiles[] = $relativeDir . $f;//$ctime;
			    	}
			    }
			    
			    if (count ($displayFiles) > 0)
			    {
			    	//die_a($displayFiles);
			    	// create a new gallery
			    	// $data =
			    	$c['category'] = -2;
			    	$c['poster_id'] = $posterId;
			    	$c['created'] = time();
			    	$c['language'] = get_language_index($article['article_language']);
			    	$c['title'] = $article['article_title'];
			    	$c['status'] = 0; //published
			    	$metadata = array("poster_name" => $u['name'], 
			    					   "content_title" => $article['article_title'],
			    					   "content_module" => "event",
			    					   "content_id" => $id
			    	);
			    	$images = array();
			    	foreach ($displayFiles as $f)
			    	{
			    		$images[] = array('filename' => str_replace("/public/ustore/", "/public/ufiles/", $f),
			    					'title' => $c['title']
			    					);
			    	}
			    	$c['thumb'] = str_replace("/public/ustore/", "/public/ufiles/", $images[0]['filename']);
					$c['metadata'] = serialize($metadata);
					$c['images'] = serialize($images);

//			    	$sql = sprintf("INSERT INTO `galleries` 
//			    		(poster_id,created, language,category 
//			    		title,status,thumb,
//			    		metadata,
//			    		images)
//			    		VALUES 
//			    		(%d, %d,%d,%d,
//			    		'%s', %d, '%s',
//			    		'%s', 
//			    		'%s')",
//			    		 $c['poster_id'], $c['created'], $c['language'], $c['category'],
//			    		 $c['title'], $c['status'], $c['thumb'],
//			    		 $c['metadata'],
//			    		 $c['images']
//			    		 /*  
//			    		 $db->quote($c['metadata'], 'STRING'),
//			    		 $db->quote($c['images'], 'STRING') */
//			    		);     	
			    	echo $id . "----<br/>"; 
					$db->insert("galleries", $c);
					$galleryId = $db->lastInsertId();
			    	$p->gallery = $galleryId;
			    	$p->thumb = $c['thumb'];
			    }
			    else
			    {
			    	//echo "No image in the gallery";
			    }
			}
    	}
    	//end for. Finished looping through all places
	    die('finish');
	} 
	

	public function updatePlaceGalleryAction()
	{
		$db = Zend_Db_Table::getDefaultAdapter() ;
	
    	$id = 0;
	    $uModel = new user_Model_User();

    	for ($id = 0; $id < 12000; $id++)
    	{
    	try{
    		$p = $this->mPlace->findByPrimary($id);
    		if (!$p || $p['checked'])
    			continue;
    
	    	$posterId = $p['poster_id'];
	    	$u = $uModel->findByPrimary($posterId);
	    	
	    	$article = $this->mArticle->findByPrimary($p['article_id']);
	    	///public/ufiles/17/images/medium/event/14_choi_trau.jpg
	    	$relativeDir = "/public/ustore/$posterId/images/original/place/";
    		$dir = APP_PATH. $relativeDir;
			if(file_exists($dir))
			{
			    $files = scandir($dir);
			    $displayFiles = array();
			    foreach ($files as $f)
			    {
			    	if (strpos ($f, $id ."_") === 0)
			    	{
	 		    		$ctime = filectime($dir . $f); 
	 		    		$displayFiles[] = $relativeDir . $f;//$ctime;
			    	}
			    }
			    
			    if (count ($displayFiles) > 0)
			    {
//			    	die_a($displayFiles);
			    	// create a new gallery
			    	// $data =
			    	$c['category'] = -1;
			    	$c['poster_id'] = $posterId; 
			    	$c['created'] = time();
			    	$c['language'] = get_language_index($article['article_language']);
			    	
			    	$c['title'] = $article['article_title'];
			    	$c['status'] = 0; //published
			    	$metadata = array("poster_name" => $u['name'], 
			    					   "content_title" => $article['article_title'],
			    					   "content_module" => "place",
			    					   "content_id" => $id
			    	);
			    				    	$images = array();
			    	foreach ($displayFiles as $f)
			    	{
			    		$images[] = array('filename' => str_replace("/public/ustore/", "/public/ufiles/", $f),
			    					'title' => $c['title']
			    					);
			    	}
			    	$c['thumb'] = str_replace("/public/ustore/", "/public/ufiles/", $images[0]['filename']);
					$c['metadata'] = serialize($metadata);
					$c['images'] = serialize($images);
					//die_a($c);

//					$sql = sprintf("INSERT INTO `galleries` 
//			    		(poster_id,created, language, 
//			    		title,status,thumb,
//			    		metadata,
//			    		images)
//			    		VALUES 
//			    		(%d, %d,%d,
//			    		'%s', %d, '%s',
//			    		'%s', 
//			    		'%s')",
//			    		 $c['poster_id'], $c['created'], $c['language'],
//			    		 $c['title'], $c['status'], $c['thumb'],
//			    		 $c['metadata'],
//			    		 $c['images']
//			    		 /*  
//			    		 $db->quote($c['metadata'], 'STRING'),
//			    		 $db->quote($c['images'], 'STRING') */
//			    		);    
//			    	
			    	echo $id . "----<br/>";
					//$db->query($sql);
					
					//$galleryId = mysql_insert_id();//$db->lastInsertId();
					$db->insert("galleries", $c);
					$galleryId = $db->lastInsertId();
			    	$p->gallery = $galleryId;
			    	$p->thumb = $c['thumb'];
			    	// move files to gallery
			    	// attach this gallery to this place
			    }
			    else
			    {
			    	//echo "No image in the gallery";
			    }
			}
			$p->checked = 1;
	    	$p->save();
    	}
    	catch (Exception $e)
    	{
    		echo $id; die($e->getMessage());
    	}	
    	}    	//end for. Finished looping through all places
	    die('finish');
	} 
		
	/*
	 * Update gallery thumb AND the place,event thumb as well
	 */
	public function updateGalleryThumbAction()
	{
		$db = Zend_Db_Table::getDefaultAdapter() ;
    	$id = 0;

    	for ($id = 0; $id < 2000; $id++)
    	{
    		//require_once(APP_PATH . "/modules/node/Model/NodeService.php");
			$nService = new node_Model_NodeService();
			$nodeTypeInfo = $nService->getNodeTypeInfo("gallery");
			$ret = $nService->getNodeData($nodeTypeInfo, $id);
			if ($ret['success'])
				$gallery = $ret['node'];
			else
			{
				continue;
			}
			$images = unserialize($gallery['images']);
			//die_a($images);
			$thumb = $images[0]['filename'];
			try 
			{
				$gallery->thumb = $thumb;
				$gallery->save();
			}
			catch (Exception $e)
			{
				die($e->getMessage());
			} 
    	}
    	//end for. Finished looping through all places
	    die('finish');
	} 
		
	
}
?>
